<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 18-11-1
 * Time: 下午2:45
 */

namespace App\HttpController;


use App\Utility\Pool\RedisPool;
use EasySwoole\Component\Pool\PoolManager;
use Swoole\Coroutine\Redis;
use EasySwoole\EasySwoole\Config;

class BaseWithRedis extends Base
{
    protected $redis;

    function onRequest(?string $action): ?bool
    {
        $redis = PoolManager::getInstance()->getPool(RedisPool::class)->getObj(Config::getInstance()->getConf('REDIS.POOL_TIME_OUT'));
        if ($redis) {
            $this->redis = $redis;
        } else {
            //直接抛给异常处理，不往下
            throw new \Exception('url :'.$this->request()->getUri()->getPath().' error,Redis Pool is Empty');
        }
        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }

    function gc()
    {
        PoolManager::getInstance()->getPool(RedisPool::class)->recycleObj($this->redis);
        parent::gc(); // TODO: Change the autogenerated stub
    }

    function getRedis():Redis {
        return $this->redis;
    }
}